/**
 * 
 */
package Consult.dao.model;

/**
 * @author zhaoyana
 *
 */

	import java.sql.Connection;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;

	import Consult.model.BookType;
	import Util.StringUtil;

	/**
	 * @author zhaoyana
	 *鍥句功绫诲瀷Dao绫�
	 */
	public class bookTypeDao {
	/**
	 * 澧炲姞鍥句功绫诲瀷
	 */
		public int add(Connection con,BookType booktype)throws Exception{
			String sql="insert into booktype value(null,?,?)";
			PreparedStatement p=con.prepareStatement(sql);
			p.setString(1, booktype.getBooktypeName());
			p.setString(2, booktype.getBookTypeDesc());
			return p.executeUpdate();
		}
		/**
		 * 鏌ヨ鍥句功绫诲埆
		 * @param con
		 * @param booktype
		 * @return
		 * @throws Exception
		 */
		public ResultSet list(Connection con,BookType bookType)throws Exception{
			StringBuffer s=new StringBuffer("select * from booktype");
			if(StringUtil.isNotEmpty(bookType.getBooktypeName())){
				s.append(" and booktypeName like '%"+bookType.getBooktypeName()+"%'");
			}
			PreparedStatement pstmt=con.prepareStatement(s.toString().replaceFirst("and", "where"));
			return pstmt.executeQuery();
		}
		
		/**
		 * 
		 * @param con
		 * @param id
		 * @return
		 * @throws Exception
		 */
		public int delete(Connection con,String id)throws Exception{
			String sql="delete from booktype where id=?";
			PreparedStatement pstmt=con.prepareStatement(sql);
			pstmt.setString(1, id);
			return pstmt.executeUpdate();
		}
		
		/**
		 
		 * @param con
		 * @param bookType
		 * @return
		 * @throws Exception
		 */
		public int update(Connection con,BookType bookType)throws Exception{
			String sql="update booktype set booktypeName=?,bookTypeDesc=? where id=?";
			PreparedStatement pstmt=con.prepareStatement(sql);
			pstmt.setString(1, bookType.getBooktypeName());
			pstmt.setString(2, bookType.getBookTypeDesc());
			pstmt.setInt(3, bookType.getId());
			return pstmt.executeUpdate();
		}
	}



